import 'package:flutter/material.dart';
import '../../config/theme/theme_manager.dart';
import 'common_button.dart';

/// 通用空状态组件
/// 用于显示无数据、无网络、错误等状态
class CommonEmpty extends StatelessWidget {
  /// 空状态类型
  final EmptyType type;

  /// 自定义图标
  final IconData? icon;

  /// 自定义图片路径
  final String? imagePath;

  /// 标题文本
  final String? title;

  /// 描述文本
  final String? description;

  /// 按钮文本
  final String? buttonText;

  /// 按钮点击回调
  final VoidCallback? onButtonPressed;

  /// 自定义图标颜色
  final Color? iconColor;

  /// 自定义图标大小
  final double? iconSize;

  /// 自定义标题样式
  final TextStyle? titleStyle;

  /// 自定义描述样式
  final TextStyle? descriptionStyle;

  /// 内边距
  final EdgeInsetsGeometry? padding;

  /// 是否显示按钮
  final bool showButton;

  /// 按钮类型
  final ButtonType buttonType;

  /// 按钮颜色
  final Color? buttonColor;

  /// 垂直间距
  final double spacing;

  const CommonEmpty({
    super.key,
    this.type = EmptyType.noData,
    this.icon,
    this.imagePath,
    this.title,
    this.description,
    this.buttonText,
    this.onButtonPressed,
    this.iconColor,
    this.iconSize,
    this.titleStyle,
    this.descriptionStyle,
    this.padding,
    this.showButton = true,
    this.buttonType = ButtonType.primary,
    this.buttonColor,
    this.spacing = 16,
  });

  @override
  Widget build(BuildContext context) {
    final themeManager = ThemeManager.instance;
    final config = _getEmptyConfig();

    return Container(
      padding: padding ?? const EdgeInsets.all(40),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          // 图标或图片
          _buildIcon(themeManager, config),
          SizedBox(height: spacing),

          // 标题
          Text(
            title ?? config.title,
            style: titleStyle ??
                TextStyle(
                  fontSize: 18,
                  fontWeight: FontWeight.w600,
                  color: themeManager.contrastColor,
                ),
            textAlign: TextAlign.center,
          ),
          SizedBox(height: spacing / 2),

          // 描述
          Text(
            description ?? config.description,
            style: descriptionStyle ??
                TextStyle(
                  fontSize: 14,
                  color: themeManager.contrastColor.withOpacity(0.6),
                  height: 1.5,
                ),
            textAlign: TextAlign.center,
          ),

          // 按钮
          if (showButton &&
              (onButtonPressed != null || config.defaultAction != null)) ...[
            SizedBox(height: spacing * 1.5),
            CommonButton(
              text: buttonText ?? config.buttonText,
              type: buttonType,
              backgroundColor: buttonColor ?? themeManager.primaryColor,
              onPressed: onButtonPressed ?? config.defaultAction,
              size: ButtonSize.medium,
            ),
          ],
        ],
      ),
    );
  }

  /// 构建图标
  Widget _buildIcon(ThemeManager themeManager, EmptyConfig config) {
    if (imagePath != null) {
      return Image.asset(
        imagePath!,
        width: iconSize ?? 120,
        height: iconSize ?? 120,
        fit: BoxFit.contain,
      );
    }

    return Icon(
      icon ?? config.icon,
      size: iconSize ?? 80,
      color: iconColor ??
          config.iconColor ??
          themeManager.contrastColor.withOpacity(0.3),
    );
  }

  /// 获取空状态配置
  EmptyConfig _getEmptyConfig() {
    switch (type) {
      case EmptyType.noData:
        return EmptyConfig(
          icon: Icons.inbox_outlined,
          title: '暂无数据',
          description: '当前没有任何数据\n请稍后再试或刷新页面',
          buttonText: '刷新',
          iconColor: Colors.grey,
        );
      case EmptyType.noNetwork:
        return EmptyConfig(
          icon: Icons.wifi_off_outlined,
          title: '网络连接失败',
          description: '请检查您的网络连接\n然后重试',
          buttonText: '重试',
          iconColor: Colors.orange,
        );
      case EmptyType.error:
        return EmptyConfig(
          icon: Icons.error_outline,
          title: '加载失败',
          description: '数据加载时出现错误\n请稍后重试',
          buttonText: '重试',
          iconColor: Colors.red,
        );
      case EmptyType.search:
        return EmptyConfig(
          icon: Icons.search_off_outlined,
          title: '未找到相关内容',
          description: '尝试使用其他关键词\n或检查拼写是否正确',
          buttonText: '重新搜索',
          iconColor: Colors.blue,
        );
      case EmptyType.permission:
        return EmptyConfig(
          icon: Icons.lock_outline,
          title: '权限不足',
          description: '您没有访问此内容的权限\n请联系管理员',
          buttonText: '返回',
          iconColor: Colors.amber,
        );
      case EmptyType.maintenance:
        return EmptyConfig(
          icon: Icons.build_outlined,
          title: '系统维护中',
          description: '系统正在维护升级\n请稍后再试',
          buttonText: '刷新',
          iconColor: Colors.purple,
        );
      case EmptyType.timeout:
        return EmptyConfig(
          icon: Icons.access_time_outlined,
          title: '请求超时',
          description: '网络请求超时\n请检查网络连接后重试',
          buttonText: '重试',
          iconColor: Colors.orange,
        );
      case EmptyType.notFound:
        return EmptyConfig(
          icon: Icons.find_in_page_outlined,
          title: '页面不存在',
          description: '您访问的页面不存在\n或已被删除',
          buttonText: '返回首页',
          iconColor: Colors.grey,
        );
    }
  }
}

/// 空状态类型
enum EmptyType {
  /// 无数据
  noData,

  /// 无网络
  noNetwork,

  /// 错误
  error,

  /// 搜索无结果
  search,

  /// 权限不足
  permission,

  /// 系统维护
  maintenance,

  /// 请求超时
  timeout,

  /// 页面不存在
  notFound,
}

/// 空状态配置
class EmptyConfig {
  final IconData icon;
  final String title;
  final String description;
  final String buttonText;
  final Color? iconColor;
  final VoidCallback? defaultAction;

  EmptyConfig({
    required this.icon,
    required this.title,
    required this.description,
    required this.buttonText,
    this.iconColor,
    this.defaultAction,
  });
}

/// 列表空状态组件
class ListEmptyState extends StatelessWidget {
  final String? title;
  final String? description;
  final String? buttonText;
  final VoidCallback? onRefresh;
  final IconData? icon;
  final bool showRefreshButton;

  const ListEmptyState({
    super.key,
    this.title,
    this.description,
    this.buttonText,
    this.onRefresh,
    this.icon,
    this.showRefreshButton = true,
  });

  @override
  Widget build(BuildContext context) {
    return CommonEmpty(
      type: EmptyType.noData,
      icon: icon,
      title: title,
      description: description,
      buttonText: buttonText,
      onButtonPressed: onRefresh,
      showButton: showRefreshButton && onRefresh != null,
      padding: const EdgeInsets.symmetric(horizontal: 40, vertical: 60),
    );
  }
}

/// 搜索空状态组件
class SearchEmptyState extends StatelessWidget {
  final String? keyword;
  final VoidCallback? onClear;
  final List<String>? suggestions;
  final ValueChanged<String>? onSuggestionTap;

  const SearchEmptyState({
    super.key,
    this.keyword,
    this.onClear,
    this.suggestions,
    this.onSuggestionTap,
  });

  @override
  Widget build(BuildContext context) {
    final themeManager = ThemeManager.instance;

    return Column(
      children: [
        CommonEmpty(
          type: EmptyType.search,
          title: keyword != null ? '未找到"$keyword"相关内容' : '未找到相关内容',
          description: '尝试使用其他关键词或检查拼写',
          buttonText: '清除搜索',
          onButtonPressed: onClear,
          showButton: onClear != null,
        ),

        // 搜索建议
        if (suggestions != null && suggestions!.isNotEmpty) ...[
          const SizedBox(height: 20),
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '搜索建议',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                    color: themeManager.contrastColor,
                  ),
                ),
                const SizedBox(height: 12),
                Wrap(
                  spacing: 8,
                  runSpacing: 8,
                  children: suggestions!.map((suggestion) {
                    return GestureDetector(
                      onTap: () => onSuggestionTap?.call(suggestion),
                      child: Container(
                        padding: const EdgeInsets.symmetric(
                          horizontal: 12,
                          vertical: 6,
                        ),
                        decoration: BoxDecoration(
                          color: themeManager.primaryColor.withOpacity(0.1),
                          borderRadius: BorderRadius.circular(16),
                          border: Border.all(
                            color: themeManager.primaryColor.withOpacity(0.3),
                          ),
                        ),
                        child: Text(
                          suggestion,
                          style: TextStyle(
                            fontSize: 12,
                            color: themeManager.primaryColor,
                          ),
                        ),
                      ),
                    );
                  }).toList(),
                ),
              ],
            ),
          ),
        ],
      ],
    );
  }
}

/// 网络错误状态组件
class NetworkErrorState extends StatelessWidget {
  final VoidCallback? onRetry;
  final String? message;

  const NetworkErrorState({
    super.key,
    this.onRetry,
    this.message,
  });

  @override
  Widget build(BuildContext context) {
    return CommonEmpty(
      type: EmptyType.noNetwork,
      description: message,
      onButtonPressed: onRetry,
      showButton: onRetry != null,
    );
  }
}

/// 加载错误状态组件
class LoadErrorState extends StatelessWidget {
  final VoidCallback? onRetry;
  final String? error;

  const LoadErrorState({
    super.key,
    this.onRetry,
    this.error,
  });

  @override
  Widget build(BuildContext context) {
    return CommonEmpty(
      type: EmptyType.error,
      description: error ?? '数据加载失败，请重试',
      onButtonPressed: onRetry,
      showButton: onRetry != null,
    );
  }
}
